﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class PrdtListByClass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            //"select * from dbo.ProductType where ID in (select ProductTypeID  from dbo.Product where Brand='samsung'"
            //"select * from dbo.producttype where ID in "
            if (Request.QueryString["typeId"] != null && Request.QueryString["typeName"] != null)
            {
                hidTypeId.Value = Request.QueryString["typeId"];
                InitialPage();
            }
        }
    }

    void InitialPage()
    {
        string filter = " AND ProductTypeID=" + Convert.ToInt32(Request.QueryString["typeId"]);
        BindListView(filter);

        BindBrand();

        BindTimeRange();

        BindSupplyer();
    }

    private void BindSupplyer()
    {
        lvSupplyer.DataSource = new GoShopping.BLL.ProductSuplyBean().GetAllProductSuplyList();
        lvSupplyer.DataBind();
    }

    private void BindBrand()
    {
        lvBrandList.DataSource = new GoShopping.BLL.DicBrandBean().GetAllBrand();
        lvBrandList.DataBind();
    }

    private void BindTimeRange()
    {
        List<string> timeRangeList = new List<string>();
        timeRangeList.Add("100以下");
        timeRangeList.Add("100-500");
        timeRangeList.Add("501-1000");
        timeRangeList.Add("1000以上");
        lvPriceRange.DataSource = timeRangeList;
        lvPriceRange.DataBind();
    }

    protected void lvBrandList_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Brand")
        {
            string filter = " AND ProductTypeID=" + Convert.ToInt32(Request.QueryString["typeId"]) + " AND Brand='" + e.CommandArgument.ToString() + "'";
            BindListView(filter);
        }
    }

    private void BindListView(string filter)
    {
        Cache["filter"] = filter;
        CommonProductsList1.LVPrdts.DataSource = pb.GetProductList(filter);
        CommonProductsList1.LVPrdts.DataBind();
    }          
    GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
    protected void lvPriceRange_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "TimeRange")
        {
            Range rg = Analyse(e.CommandArgument.ToString());
            string filter = " AND ProductTypeID=" + Convert.ToInt32(Request.QueryString["typeId"]) + " AND Price BETWEEN " + rg.IntMin + " AND " + rg.IntMax;
            BindListView(filter);
        }
    }
    protected void lvSupplyer_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Supplyer")
        {
            string filter = " AND ProductTypeID=" + Convert.ToInt32(Request.QueryString["typeId"]) + " AND SupplyerID='" + e.CommandArgument.ToString() + "'";
            BindListView(filter);
        }
    }
    private Range Analyse(string p)
    {
        Range rg = new Range();
        if (p.Contains("以下"))
        {
            p = p.Replace("以下", "");
            rg.IntMax = Convert.ToInt32(p);
            rg.IntMin = 0;
        } 
        if (p.Contains("以上"))
        {
            p = p.Replace("以上", "");
            rg.IntMax = 100000;
            rg.IntMin =Convert.ToInt32(p);
        }
        if (p.Contains("-"))
        {
            string[] arr = p.Split('-');
            rg.IntMax = Convert.ToInt32(arr[1]);
            rg.IntMin = Convert.ToInt32(arr[0]);
        }
        return rg;
        
    }
}
